package com.dreamchaser.loginTest.filter;


import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 敏感词过滤拦截器
 */
public class WordFilter implements Filter {
    static final String[] sensitiveWords={"敏感词1","脏话","骂人"};
    static final String harmoniousWord="***";

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        req.setCharacterEncoding("utf-8");
        // 将req resp 转为子接口的类型
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)resp;
        String word=req.getParameter("word");
        for (String s:sensitiveWords){
            word=word.replaceAll(s,harmoniousWord);
        }
        request.setAttribute("word",word);
        chain.doFilter(req, resp);
    }
}
